<template>
	<view class="page">
		<view class="top-view">
			<view class="search-wrap flexACB">
				<view class="search-item">
					<view class="all" @click="allChoose">
						<view class="check" :class="checked?'':'bor'">
							<u-icon v-if="checked" name="checkmark-circle-fill" color="#eb2224" size="23"></u-icon>
						</view>
						<text class="t">全选</text>
					</view>
				</view>
				
				<view class="search-item" @click="provincesShow = true">
					<view class="text">
						{{queryParams.province||'地区'}}
					</view>
					<image class="down" src="../../../static/img/icon-down.png" mode="widthFix"></image>
				</view>
				
				<view class="search-item" @click="chooseHy">
					<view class="text">
						{{queryParams.type?queryParams.typeName : '行业筛选'}}
					</view>
					<image class="down" src="../../../static/img/icon-down.png" mode="widthFix"></image>
				</view>
				<view class="search-item" @click="chooseHyfl">
					<view class="text">
						{{queryParams.hyflParent?queryParams.hyflParent : '行业分类'}}
					</view>
					<image class="down" src="../../../static/img/icon-down.png" mode="widthFix"></image>
				</view>
				<view class="search-item">
					<view class="text">
						地图
					</view>
				</view>
			</view>
			<view class="classify-type">
				<u-scroll-list :indicator="false">
					<view class="item" :class="queryParams.hyfl === item.id?'active':''"
						v-for="(item, index) in productTypeList1" :key="index" @click="changeType1(item.id)">
						{{item.typeName}}
					</view>
				</u-scroll-list>
			</view>
		</view>
		<view class="page-tips">
			群发消息需选注册名片，方便用户联系你
		</view>
		<view class="main">
			<view class="user-card" v-for="(item,index) in list" :key="index" @click="checkBtn(item)">
				<!-- <image class="check" src="../../../static/img/check-no.png" mode="widthFix"></image> -->
				<view class="check" :class="item.checked?'':'bor'">
					<u-icon v-if="item.checked" name="checkmark-circle-fill" color="#eb2224" size="24"></u-icon>
				</view>
				<view class="top">
					<view class="tips">
						<view class="tip flexAJC" style="background: #FFE6AE;color: #E1A826;">
							{{item.sign||'--'}}
						</view>
						<view class="tip flexAJC" style="background: #FFC3C3;color: #D73030;">
							<!-- 0-经销商；1-厂家；2-产业链 -->
							<text v-if="item.type==0">经销商</text>
							<text v-if="item.type==1">厂家</text>
							<text v-if="item.type==2">产业链</text>
							
						</view>
						<view class="tip flexAJC" style="background: #B2CEFF;color: #326ED5;">
							已认证
						</view>
					</view>
					<view class="user">
						<view class="name-wrap">
							<view class="name">
								{{item.name}}
							</view>
							<view class="label">
								{{item.position}}
							</view>
							<image v-if="item.sfVip" class="vip" src="../../../static/img/vip-icon.png" mode="widthFix"></image>
						</view>
						<view class="ads">
							<image class="icon" src="../../../static/img/gongsi.png" mode="widthFix"></image>
							{{item.qyName}}
						</view>
						<view class="ads">
							<image class="icon" src="../../../static/img/dizhi.png" mode="widthFix"></image>
							{{item.address}}
						</view>
					</view>
					<view class="avatar">
						<image class="tx" :src="fileServer+item.headImg" mode="aspectFill"></image>
						<view class="text">
							已有<text style="font-weight: bold;">{{item.lxNum||0}}</text>人联系
						</view>
					</view>
				</view>
				<view class="content">
					主营品牌产品：{{item.zypphcp}}
				</view>
				<view class="content">
					销售渠道：{{item.xsqd}}
				</view>
				<view class="btn-list">
					<view class="btn flexAJC" style="background-color: #43B83E;">
						<image class="icon" mode="widthFix" src="../../../static/img/whatsappIcon.png"></image>
						语音
					</view>
					<view class="btn flexAJC" style="background-color: #FA9932;">
						<image class="icon" mode="widthFix" src="../../../static/img/shipin-white.png"></image>
						视频
					</view>
					<view class="btn flexAJC" style="background-color: #4FBFE7;">
						<image class="icon" mode="widthFix" src="../../../static/img/dizhi-white.png"></image>
						导航
					</view>
					<view class="btn flexAJC" style="background-color: #598FEE;">
						<image class="icon" mode="widthFix" src="../../../static/img/Telephone-white.png"></image>
						电话
					</view>
					<view class="btn flexAJC" style="background-color: #16C40B">
						<image class="icon" mode="widthFix" src="../../../static/img/liaoyilia-green.png"></image>
						聊聊
					</view>
				</view>
			</view>
		
			<u-empty v-if="status!='loading'&&list.length==0"
			        mode="data"
			        icon="http://cdn.uviewui.com/uview/empty/list.png"
			>
			</u-empty>
			<u-loadmore :status="status" v-else />
		</view>

		<view class="btm-btn-tc">

		</view>
		<view class="btm-btn" @click="forward()">
			转发
		</view>
		<u-popup :show="show" mode="bottom" round="24rpx" overlayOpacity="0">
			<view class="pop-wrap">
				<image class="close" @click="show = false" src="../../../static/img/icon-down.png" mode="widthFix"></image>
				<view class="pop-title">
					转发内容
				</view>
				<u-textarea v-model="value1" placeholder="请输入信息内容"></u-textarea>
				<view class="pop-title" style="margin-top: 16rpx;">
					添加推广产品
				</view>
				<!-- pages/client/product/product -->
				<view class="produceItem">
					<image v-if="produceObj.productImg" class="img" :src="fileServer+produceObj.productImg" mode="widthFix"></image>
					<view class="add-img" @click="chooseP('/pages/client/product/product')">
						<view class="icon">
							<u-icon name="plus" color="#ABABAB" size="26"></u-icon>
						</view>
						<view class="add-txt">添加产品</view>
					</view>
				</view>
				<view class="total">
					<view class="item">
						共选<text>{{chooseList.length}}</text>个客户
					</view>
					<view class="item">
						需消耗<text>{{consume}}</text>积分
					</view>
					<view class="item">
						积分余额：<text>{{Zfobj.jfye}}</text>
					</view>
					<view class="item">
						<text>购买积分</text>
						<image class="rit" src="../../../static/img/right-red.png" mode="widthFix"></image>
					</view>
				</view>
				<view class="submit-btn flexAJC" @click="submit">
					立即发布
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				fileServer: this.$fileServer,
				show: false,
				value1: '',
				fileList: [],
				queryParams: {
					province: '',
					type: '',
					typeName: '',
					hyflParent: '',
					hyflIndex: '',
					hyflItemIndex: '',
					hyfl: '',
				},
				hyList:[],//行业分类
				list:[],
				status:'loading',//loading / nomore
				pageIndex:1,
				total:0,
				provinces: getApp().globalData.provincesList,//产地
				provincesShow: false,
				productTypeList1:[],
				memberInfo:{},//个人信息
				consume:0,//消耗积分
				chooseList:[],//选中客户
				checked:false,//全选
				produceObj:{},//选择产品信息
				Zfobj:{},//
			};
		},
		onLoad() {
			this.getList();
			this.$api.member.getJifenIndex().then(res => {
				console.log(res)
				if(res.flag){
					let info = res.data;
					this.memberInfo = info.member
					this.taskList = info.taskList
				}
			})
			this.$api.talk.getZfxxJifen({code:'zhuanfaProductCode'}).then(res => {
				console.log(res)
				if(res.flag){
					this.Zfobj = res.data;
				}
			})
			
		},
		methods: {
			// 地区
			chooseProvinces(e) {
				console.log(e)
				this.queryParams.province = e.value[0]
				this.provincesShow = false;
				this.pageIndex = 1;
				this.getList()
			},
			// 行业分类
			chooseHyfl() {
				// console
				if(this.hyList.length==0){
					uni.showToast({
						title: '请先选择行业筛选',
						icon: "none"
					})
					return false
				}
				uni.showActionSheet({
					itemList: this.hyList.map(i => {
						return i.hyflName
					}),
					success: (res) => {
						this.queryParams.hyflItemIndex = ''
						this.queryParams.hyflParent = this.hyList[res.tapIndex].hyflName
						this.queryParams.hyflIndex = res.tapIndex
						this.getProductTypeList(this.hyList[res.tapIndex].data[0].id)
					}
				})
			
			},
			// 行业筛选
			chooseHy() {
				uni.showActionSheet({
					itemList: getApp().globalData.typeList.map(i => {
						return i.label
					}),
					success: (res) => {
						this.queryParams.type = getApp().globalData.typeList[res.tapIndex].value
						this.queryParams.typeName = getApp().globalData.typeList[res.tapIndex].label
						this.queryParams.hyflItemIndex = '';
						this.queryParams.hyflParent = '';
						this.queryParams.hyflIndex = '';
						this.productTypeList1 = [];
						this.getHylbList();
						this.pageIndex = 1
						this.getList()
						// this.$forceUpdate()
					}
				})
			},
			changeType1(type) {
				this.queryParams.hyfl = type;
				this.pageIndex = 1
				this.getList()
			},
			getList(){
				this.status = 'loading';
				let pageIndex = this.pageIndex;
				let queryParams = {...this.queryParams,pageIndex}
				this.$api.mp.getMpZkhList(queryParams).then(res => {
					uni.stopPullDownRefresh(); // 关闭下拉刷新 
					if(res.code==200){
						let list = res.rows;
						list.forEach(ele=>{
							ele.checked = false;
						})
						if(pageIndex==1){
							this.list = list
						}else{
							this.list = this.list.concat(list)
						}
						this.total = res.total;
						if(this.list.length<res.total){
							this.status = 'loadmore';
						}else{
							this.status = 'nomore';
						}
					}
				})
			},
			// 选择
			checkBtn(item){
				item.checked = !item.checked
			},
			// 全选
			allChoose(){
				this.checked = !this.checked;
				this.list.forEach(ele=>{
					ele.checked = this.checked;
				})
			},
			getHylbList() {
				this.$api.company.getHylbList({
					type: this.queryParams.type
				}).then(res => {
					this.hyList = res.data
				})
			},
			getProductTypeList(id) {
				this.$api.company.getProductTypeList({
					productTypeId: id
				}).then(res => {
					this.productTypeList1 = res.data
				})
			},
			// 转发
			forward(){
				var arr = this.list.filter(ele=>ele.checked);
				if(arr.length==0){
					uni.showToast({
						title: '请至少选择一个客户',
						icon: "none"
					})
					return false
				}
				let consume = 0;
				this.consume = this.Zfobj.zfPrice*arr.length;
				this.chooseList = arr;
				if(this.Zfobj.jfye-this.consume>0){
					this.show = true;
				}else{
					uni.showToast({
						title: '积分不足',
						icon: "none"
					})
				}
			},
			// 选择产品
			chooseP(url){
				let _this = this;
				let companyId = this.memberInfo.companyId;
				uni.navigateTo({
					url:`${url}?companyId=${companyId}`,
					events:{
						someEvent: function(data) {
						  console.log(data)
						  _this.produceObj = data
						}
					}
				})
			},
			// 发布
			submit(){
				// 
				if(!this.value1){
					uni.showToast({
						title: '请输入转发内容',
						icon: "none"
					})
					return false
				}
				if(!this.produceObj.id){
					uni.showToast({
						title: '请选择产品',
						icon: "none"
					})
					return false
				}
				let productId = this.produceObj.id;
				let jsrIdList = [];
				this.chooseList.forEach(ele=>{
					jsrIdList.push(ele.id)
				})
				
				this.$api.talk.zhufanProduct({
					content:this.value1,
					productId,
					jsrId:jsrIdList.join(',')
				}).then(res => {
					console.log(res)
					uni.showToast({
						title: res.msg,
						icon: "none"
					})
					if(res.flag){
						this.show = true;
						uni.navigateBack()
					}
				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList`].length
				lists.map((item) => {
					this[`fileList`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList`][fileListLen]
					this[`fileList`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		background-color: #F5F5F5;min-height: 100vh;

		.top-view {
			width: 100%;
			background-color: #fff;
			padding: 24rpx 32rpx 0;position: sticky;top: 0;z-index: 100;

			.search-wrap {
				width: 100%;
				padding: 0 0 16rpx;

				.search-item {
					height: 44rpx;
					display: flex;
					align-items: center;

					.text {
						font-size: 32rpx;
						line-height: 44rpx;

					}

					.all {display: flex;align-items: center;
						.t{font-size: 32rpx;font-weight: bold;color: #EC2424;margin-left: 5rpx;}
						.check {
							width: 40rpx;
							height: 40rpx;border-radius: 50%;
							&.bor{border: 1px solid #909090;background-color: #fff;}
						}
					}

					.down {
						width: 29rpx;
						height: auto;
					}
				}
			}

			.classify-type {
				.item {
					width: auto;
					height: 56rpx;
					background: #EBEBEB;
					border-radius: 28rpx;
					padding: 0 12rpx;
					display: inline-block;
					margin-right: 20rpx;
					font-size: 28rpx;
					white-space: nowrap;
					line-height: 56rpx;
					&.active {
						background: #EC2424;
						color: #fff;
					}
				}
			}

		}

		.main {
			position: relative;
			padding: 20rpx;
			overflow: hidden;

			.user-card {
				width: 100%;
				background: #FFFFFF;
				border-radius: 16rpx;
				padding: 24rpx;
				margin-bottom: 24rpx;
				position: relative;

				.check {
					width: 44rpx;
					height: 44rpx;
					position: absolute;
					z-index: 3;
					top: 4rpx;
					left: 0;border-radius: 50%;
					&.bor{border: 1px solid #909090;background-color: #fff;}
				}

				.top {
					width: 100%;
					display: flex;
					padding-bottom: 16rpx;
					margin-bottom: 18rpx;
					border-bottom: 1px solid #F0F0F0;

					.tips {
						width: 118rpx;
						border-radius: 6rpx;
						overflow: hidden;
						flex-shrink: 0;
						position: relative;

						.tip {
							width: 100%;
							height: 40rpx;
						}
					}

					.user {
						flex: 1;
						padding: 0 0 0 12rpx;

						.name-wrap {
							line-height: 44rpx;

							.name {

								font-size: 32rpx;
								font-weight: bold;
								margin-right: 10rpx;
								display: inline-block;
							}

							.label {
								font-size: 24rpx;
								color: #AAAAAA;
								display: inline-block;
								margin-right: 6rpx;
							}

							.vip {
								display: inline-block;
								width: 34rpx;
								height: auto;
							}

							.dept {
								width: auto;
								display: inline-block;
								height: 36rpx;
								border-radius: 18rpx;
								border: 1px solid #DE3333;
								color: #DE3333;
								font-size: 24rpx;
								padding: 0 20rpx;
								line-height: 34rpx;
							}
						}

						.ads {
							color: #777777;
							font-size: 28rpx;
							line-height: 40rpx;

							.icon {
								display: inline-block;
								width: 24rpx;
								height: auto;
								margin-right: 6rpx;
							}
						}
					}

					.avatar {
						flex-shrink: 0;
						display: flex;
						flex-direction: column;
						align-items: flex-end;

						.tx {
							width: 48rpx;
							height: 48rpx;
							border-radius: 50%;
						}

						.text {
							margin-top: 40rpx;
							font-size: 24rpx;
						}
					}

					.avatar-left {
						width: 88rpx;
						height: 88rpx;
						position: relative;

						.tx {
							width: 100%;
							height: 100%;
							border-radius: 50%;
						}

						.vip {
							position: absolute;
							right: 0;
							top: 0;
							width: 28rpx;
							height: auto;
						}
					}

					.talk-btn {
						width: 120rpx;
						height: 50rpx;
						background: #16C40B;
						border-radius: 25rpx;
						color: #FFFFFF;
						font-size: 24rpx;
						align-self: center;

						.icon {
							width: 24rpx;
							height: auto;
							margin-right: 4rpx;
						}
					}

				}

				.content {
					color: #AAAAAA;
					line-height: 40rpx;
					font-size: 28rpx;
					margin-bottom: 10rpx;
				}

				.btn-list {
					margin-top: 10rpx;
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: space-between;

					.btn {
						width: 120rpx;
						height: 50rpx;
						font-size: 24rpx;
						color: #FFFFFF;
						border-radius: 25rpx;

						.icon {
							width: 24rpx;
							height: auto;
							margin-right: 4rpx;
						}
					}
				}

				.content-text {
					padding: 20rpx 0 0 0;
					font-size: 28rpx;
					border-top: 1px solid #F0F0F0;
				}
			}
		}

		.page-tips {
			width: 100%;
			background: #FCF5EF;
			color: #CC6C6C;
			height: 72rpx;
			line-height: 72rpx;
			text-align: center;
			font-weight: bold;
		}

	}

	.btm-btn-tc {
		height: 116rpx;
		width: 100%;
	}

	.btm-btn {
		width: 684rpx;
		height: 70rpx;
		background: #EC2424;
		border-radius: 42rpx;
		margin: 0 auto;
		display: flex;
		align-items: center;
		justify-content: center;
		color: #FFFFFF;
		font-size: 28rpx;
		font-weight: bold;
		position: fixed;
		bottom: 46rpx;
		left: 50%;
		transform: translateX(-50%);
		z-index: 2;

	}

	.pop-wrap {
		width: 100%;
		padding: 28rpx 30rpx;
		border-radius: 24rpx 24rpx 0 0;
		border-top: 1px solid rgba(0,0,0,0.08);

		.close {
			display: block;
			margin: 0 auto 24rpx;
			width: 40rpx;
			height: 22rpx;
		}

		.pop-title {
			margin-bottom: 14rpx;
			font-size: 28rpx;
			font-weight: bold;
		}
		.produceItem{display: flex;align-items: center;
			.img{width: 144rpx;border-radius: 10rpx;height: 144rpx;background-color: #ededed;margin-right: 10rpx;}
		}
		.add-img {
			width: 144rpx;
			height: 144rpx;background-color: #ededed;border-radius: 10rpx;
			position: relative;
			.icon{display: flex;justify-content: space-around;padding-top: 30rpx;margin-bottom: 5rpx;}
			.add-txt {color: #AAAAAA;font-size: 26rpx;text-align: center;}
		}

		.total {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin-top: 24rpx;
			width: 100%;
			margin-bottom: 38rpx;

			.item {
				display: flex;
				align-items: center;
				color: #777777;

				text {
					color: #EC2424;
				}

				.rit {
					width: 16rpx;
					height: auto;
				}
			}
		}

		.submit-btn {
			width: 244rpx;
			height: 70rpx;
			background: #EC2424;
			border-radius: 42rpx;
			font-size: 26rpx;
			color: #FFFFFF;
			font-weight: bold;
			margin: 0 auto;
		}
	}
</style>